"""
@Author：WangYuXiang
@E-mile：Hill@3io.cc
@CreateTime：2021/4/22 17:03
@DependencyLibrary：无
@MainFunction：无
@FileDoc： 
    qr.py
    文件说明
@ChangeHistory:
    datetime action why
    example:
    2021/4/22 17:03 change 'Fix bug'
        
"""
from common.accessory.models import AccessoryModel
from srf.fields import SerializerMethodField
from srf.serializers import ModelSerializer


class AccessoryLocalSerializer(ModelSerializer):
    download_url = SerializerMethodField(method_name='get_download_url')

    class Meta:
        model = AccessoryModel
        fields = ('id', 'file_name', 'file_note')
        read_only_fields = ('id',)

    async def get_download_url(self, obj: AccessoryModel):
        return obj.get_download_url()

    async def save(self, **kwargs):
        print(self.context)
        request = self.context['request']
        file = request.files.get('file')
        instance = await AccessoryModel.request_file_save_to_local(request, file, **self.validated_data)
        self.instance = instance
        return instance
    async def create(self,):
        pass
    async def update(self,):
        pass

class AccessoryLocalListSerializer(ModelSerializer):
    class Meta:
        model = AccessoryModel
        exclude = ('create_time', 'update_time', 'file_path', 'is_net', 'file_key')
        read_only_fields = ('id',)
